
package com.library.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.library.common.base.ApiResult;
import com.library.entity.BookTypeEntity;
import com.library.service.BookTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 中图法分类表
 *
 * @author pig
 * @date 2023-09-26 09:27:21
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("ruralLibrary/bookType")
@Api(tags = "中图法分类表管理", value = "中图法分类表管理")
public class BookTypeController {

    @Resource
    private BookTypeService bookTypeService;

    @ApiOperation(value = "分页查询")
    @GetMapping("/page")
    public ApiResult getbookTypePage(Page page, BookTypeEntity bookType) {
        LambdaQueryWrapper<BookTypeEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.like(StrUtil.isNotBlank(bookType.getBookType()), BookTypeEntity::getBookType, bookType.getBookType());
        return ApiResult.ok(bookTypeService.page(page, wrapper));
    }

    /**
     * 通过id查询中图法分类表
     *
     * @param id id
     * @return ApiResult
     */
    @ApiOperation(value = "通过id查询")
    @GetMapping("/{id}")
    public ApiResult getById(@PathVariable("id") Long id) {
        return ApiResult.ok(bookTypeService.getById(id));
    }

    /**
     * 新增中图法分类表
     *
     * @param bookType 中图法分类表
     * @return ApiResult
     */
    @ApiOperation(value = "新增中图法分类表")
    @PostMapping("/add")
    public ApiResult save(@RequestBody BookTypeEntity bookType) {
        bookType.setType("1");
        return ApiResult.ok(bookTypeService.save(bookType));
    }

    /**
     * 修改中图法分类表
     *
     * @param bookType 中图法分类表
     * @return ApiResult
     */
    @ApiOperation(value = "修改中图法分类表")
    @PostMapping("/updateById")
    public ApiResult updateById(@RequestBody BookTypeEntity bookType) {
        return ApiResult.ok(bookTypeService.updateById(bookType));
    }

    /**
     * 通过id删除中图法分类表
     *
     * @param ids id列表
     * @return ApiResult
     */
    @ApiOperation(value = "通过id删除中图法分类表")
    @GetMapping("/delete")
    public ApiResult removeById(@RequestBody Long[] ids) {
        return ApiResult.ok(bookTypeService.removeByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     *
     * @param bookType 查询条件
     * @return excel 文件流
     */
    @GetMapping("/export")
    public List<BookTypeEntity> export(BookTypeEntity bookType) {
        return bookTypeService.list(Wrappers.query(bookType));
    }

    @PostMapping("/synchronization")
    public ApiResult save() {
        return bookTypeService.synchronization();
    }

    @GetMapping("/list/{id}")
    public ApiResult list(@PathVariable("id") Long id) {
        LambdaQueryWrapper<BookTypeEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(BookTypeEntity::getDatatypeId, id);
        wrapper.orderByDesc(BookTypeEntity::getCreateTime);
        return ApiResult.ok(bookTypeService.list(wrapper));
    }

}
